package com.example.aevovein.ui.skilltree;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class RadarChartView extends View {
    private List<Integer> values = new ArrayList<>();
    private List<String> labels = new ArrayList<>();
    private int maxValue = 100;
    private Paint paint;
    private Paint textPaint;
    private Paint labelPaint;
    private float centerX;
    private float centerY;
    private float radius;
    private static final float LABEL_MARGIN = 20f; // 标签与雷达图的距离

    public RadarChartView(Context context) {
        super(context);
        init();
    }

    public RadarChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(2f);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.BLUE);
        textPaint.setTextSize(30f);

        labelPaint = new Paint();
        labelPaint.setAntiAlias(true);
        labelPaint.setStyle(Paint.Style.FILL);
        labelPaint.setColor(Color.BLACK);
        labelPaint.setTextSize(30f);
    }

    public void setValues(List<Integer> values, List<String> labels, int maxValue) {
        this.values = values;
        this.labels = labels;
        this.maxValue = maxValue;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w / 2f;
        centerY = h / 2f;
        radius = Math.min(w, h) / 2f - LABEL_MARGIN - 40f; // 减小半径以留出标签空间
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (values.isEmpty()) return;

        // 绘制背景网格
        int levels = 5;
        for (int i = 1; i <= levels; i++) {
            float currentRadius = radius * i / levels;
            Path path = new Path();
            for (int j = 0; j < values.size(); j++) {
                float angle = (float) (2 * Math.PI * j / values.size() - Math.PI / 2);
                float x = centerX + (float) (currentRadius * Math.cos(angle));
                float y = centerY + (float) (currentRadius * Math.sin(angle));
                if (j == 0) {
                    path.moveTo(x, y);
                } else {
                    path.lineTo(x, y);
                }
            }
            path.close();
            canvas.drawPath(path, paint);
        }

        // 绘制数据区域
        Path dataPath = new Path();
        for (int i = 0; i < values.size(); i++) {
            float value = values.get(i);
            float angle = (float) (2 * Math.PI * i / values.size() - Math.PI / 2);
            float x = centerX + (float) (radius * value / maxValue * Math.cos(angle));
            float y = centerY + (float) (radius * value / maxValue * Math.sin(angle));
            if (i == 0) {
                dataPath.moveTo(x, y);
            } else {
                dataPath.lineTo(x, y);
            }
        }
        dataPath.close();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#80009688"));
        canvas.drawPath(dataPath, paint);
        paint.setStyle(Paint.Style.STROKE);

        // 绘制标签
        for (int i = 0; i < labels.size(); i++) {
            float angle = (float) (2 * Math.PI * i / labels.size() - Math.PI / 2);
            float labelRadius = radius + LABEL_MARGIN;
            float x = centerX + (float) (labelRadius * Math.cos(angle));
            float y = centerY + (float) (labelRadius * Math.sin(angle));

            String label = labels.get(i);
            float textWidth = labelPaint.measureText(label);
            float textHeight = labelPaint.getTextSize();

            // 根据角度调整文本对齐方式
            float textX = x;
            float textY = y;
            Paint.Align align = Paint.Align.CENTER;

            // 根据角度调整文本位置
            if (angle < -Math.PI / 2 + 0.1 || angle > Math.PI / 2 - 0.1) {
                // 顶部
                textY = y - textHeight / 2;
            } else if (angle > -0.1 && angle < 0.1) {
                // 右侧
                textX = x + textWidth / 2;
            } else if (angle > Math.PI - 0.1 || angle < -Math.PI + 0.1) {
                // 底部
                textY = y + textHeight;
            } else {
                // 左侧
                textX = x - textWidth / 2;
            }

            labelPaint.setTextAlign(align);
            canvas.drawText(label, textX, textY, labelPaint);
        }
    }
}
